package main

import (
	_ "fly/routers"
	"fly/tasks"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/logs"
	beego "github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/task"
	"github.com/duke-git/lancet/v2/condition"
	"github.com/duke-git/lancet/v2/convertor"
	_ "github.com/go-sql-driver/mysql"
)

type LogConfig struct {
	FileName string `json:"filename"`
	Level    int    `json:"level"`
	MaxDays  int    `json:"maxdays"`
}

func init() {
	orm.Debug = beego.BConfig.RunMode == "dev"
	_ = logs.SetLogger(logs.AdapterFile, convertor.ToString(&LogConfig{
		FileName: condition.TernaryOperator(orm.Debug, "dev.log", "prod.log"),
		Level:    7,
		MaxDays:  30,
	}))

	err := orm.RegisterDriver("mysql", orm.DRMySQL)
	fmt.Printf("xxx >> register orm driver err %v \n", err)
	err = orm.RegisterDataBase("default", "mysql", beego.AppConfig.DefaultString("datasource", ""))
	fmt.Printf("xxx >> register orm database err %v \n", err)

}

func main() {
	err := orm.RunSyncdb("default", false, true)
	if err != nil {
		logs.Error(err)
		return
	}
	task.AddTask(tasks.AircraftUpdateTask())
	task.StartTask()
	defer task.StopTask()
	beego.Run()
}
